package com.study.services;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.study.model.jdao.ChangesJdao;
import com.study.model.mdao.ChangesMdao;
import com.study.model.pojo.Changes;

@Service
public class ChangesServices {
	@Autowired
	ChangesMdao cmd;
	@Autowired
	ChangesJdao jmd;
	/**
	 * 跨部门调职申请*/
	public void addChanges(Changes c){
		c.setChangeno(this.getID());
		c.setType(1);
		c.setState(0);
		cmd.addChanges(c);
	};
	
	/**
	 * 离职申请(stuff)*/
	public void addChanges2(Changes c){
		c.setChangeno(this.getID());
		c.setType(2);
		c.setState(0);
		cmd.addChanges(c);
	};
	/**
	 * 离职申请(manager)*/
	public void addChanges3(Changes c){
		c.setChangeno(this.getID());
		c.setType(2);
		c.setState(1);
		cmd.addChanges(c);
	};
	public List<Changes> findAdmiss(int eid){
		return jmd.findAdmiss(eid);
	};
	
	public List<Changes> bfindAdmiss(){
		return jmd.bfindAdmiss();
	};
	
	public List<Changes> findTransfer(int eid){
		return jmd.findTransfer(eid);
	};
	public List<Changes> findTransfer2(int eid){
		return jmd.findTransfer2(eid);
	};
	
	public List<Changes> bfindTransfer(){
		return jmd.bfindTransfer();
	};
	/**
	 * 完成处理申请*/
	public void agrChanges(String changeno){
		cmd.agrChanges(changeno);
	}
	public void agrChanges2(String changeno){
		cmd.agrChanges2(changeno);
	}
	public void disChanges(String changeno){
		cmd.disChanges(changeno);
	}
	public void disChanges2(String changeno){
		cmd.disChanges2(changeno);
	}
	
	/**
	 * 完成待办事宜**/
	public void finishChanges(String changeno){
		cmd.finishChanges(changeno);
	};
	
	/**
	 * 自动生成ID*/
	public String getID(){
		DateFormat bf = new SimpleDateFormat("yyMMdd");
		String maxid=cmd.queryMaxid(bf.format(new Date()));
		String id="";
		if(maxid != null){
			int num=Integer.parseInt(maxid.substring(9,12));
			if((num+1)/100!=0){
				id=maxid.substring(0, 9)+(num+1);
			}else if((num+1)/10!=0){
				id=maxid.substring(0, 9)+"0"+(num+1);
			}else {
				id=maxid.substring(0, 9)+"00"+(num+1);
			}
		}else{
			id="chg"+bf.format(new Date())+"001";			
		}
		return id;
	}
}
